﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StorageInterface.Dialogs
{
    /// <summary>
    /// Interaction logic for NewOrderDialog.xaml
    /// </summary>
    public partial class NewOrderDialog : Window
    {
        Partner Partner = null;
        public NewOrderDialog(Partner p)
        {
            InitializeComponent();
            Title = "Новый заказ компании " + p.Name;
            ProductCB.ItemsSource = Data.GetAllProducts();
            Partner = p;
           

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NewProductDialog npd = new NewProductDialog();
            npd.Owner = this;
            npd.ShowDialog();
            ProductCB.ItemsSource = Data.GetAllProducts();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ProductCB.SelectedItem == null ||
                    NameDocTB.Text == "" ||
                    AmountTB.Text == "" ||
                    //CommentTB.Text == "" ||
                    SummTB.Text == "" )
                   // DateTB.SelectedDate == null)
                {
                    ErrorHandler.HandleError("Одно из полей не заполнено");
                    return;
                }

                double am = 0;
                double summ = 0;

                if (!double.TryParse(AmountTB.Text, out am) || !double.TryParse(SummTB.Text, out summ))
                {
                    ErrorHandler.HandleError("Одно из полей числового типа заполнено неправильно");
                    return;
                }

                if (stateCB.Text != "Выполнен")
                    Data.NewOrder(NameDocTB.Text, CommentTB.Text, summ, am, stateCB.Text, DateTime.Now,
                        Partner, ProductCB.SelectedItem as Product);
                Close();
            }
            catch (Exception ex) { ErrorHandler.HandleError(ex.Message); }
            
        }
    }
}
